using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using ViScript.Core.Utils;

namespace ViScript.Core.Utils
{
    public class Base64SerializationBasedEscapeStrategy : IEscapeStrategy
    {
        public string Escape(string unescapedText)
        {
            MemoryStream serializationStream = new MemoryStream();
            new BinaryFormatter().Serialize(serializationStream, unescapedText);
            byte[] bytes = serializationStream.ToArray();
            return Convert.ToBase64String(bytes);
        }

        public string Unescape(string escapedText)
        {
            byte[] bytes = Convert.FromBase64String(escapedText);
            MemoryStream serializationStream = new MemoryStream(bytes);
            return (string)new BinaryFormatter().Deserialize(serializationStream);
        }
    }
}